package testClasses.xstream;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import utilities.Key;
import view.EViewList;
import view.AbstractView;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import controller.AbstractController;
import eventStrategies.IEventResponse;
import events.ECodeEventList;

public class ControllerConverter implements Converter {

	public boolean canConvert(Class clazz) {
		return AbstractController.class.isAssignableFrom(clazz);
	}

	public void marshal(Object obj, HierarchicalStreamWriter writer,
			MarshallingContext context) {
		
		System.out.println("Marshalling Controller");
		
		AbstractController controller = (AbstractController) obj;
		
		//Save key, to be matched with appropriate model
		Key key = controller.getKey();
		writer.startNode("key");
		writer.setValue(key.getValue()+"");
		writer.endNode();
		
		//Save list of eventStrategies
		HashMap<ECodeEventList, IEventResponse> eventStrategies = controller.getEvents();
		ArrayList<String> codeEventList = new ArrayList<String>();
		writer.startNode("Events");
		for (Map.Entry<ECodeEventList, IEventResponse> entry : eventStrategies.entrySet()) {
			codeEventList.add(entry.getKey().toString());
			String name = entry.getKey().toString();
			writer.setValue(name);			
		}
		
		writer.endNode();
		
		
		
		//Save other data?  (AI state information, etc.?)
	}

	public Object unmarshal(HierarchicalStreamReader reader,
			UnmarshallingContext context) {
		
		System.out.println("UNMarshalling Controller");
		
		
		return null;
	}

}
